package com.cbgolf.oa.util;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import com.cbgolf.oa.R;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;

/**
 * Created by lq on 2017/8/25.
 */

public class DialogUtil {

  public static void showQuitDialog(final Activity act, onSubmitListener listener) {
    final Dialog dialog = new Dialog(act, R.style.mDialog);
    View view = act.getLayoutInflater().inflate(R.layout.dialog_quit, null);
    View layout = view.findViewById(R.id.dialog_quit_layout_ll);
    LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) layout.getLayoutParams();
    if (!Util.isLarge()) {
      ll.width = act.getWindowManager().getDefaultDisplay()
          .getWidth() - DensityUtil.dip2px(act, 45);
    } else {
      ll.width = act.getWindowManager().getDefaultDisplay()
          .getWidth() / 16 * 9;
    }
    ll.height = LayoutParams.WRAP_CONTENT;
    layout.setLayoutParams(ll);

    TextView cancelTv = (TextView) view.findViewById(R.id.dialog_quit_cancel_tv);
    TextView sureTv = (TextView) view.findViewById(R.id.dialog_quit_sure_tv);
    AutoUtil.auto(view);
    dialog.setContentView(view);
    sureTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.cancel();
        dialog.dismiss();
        if(null != listener) {
          listener.doSubmit();
        }

//                act.overridePendingTransition(R.anim.in_from_left, R.anim.out_from_right);
      }
    });
    cancelTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.cancel();
        dialog.dismiss();
      }
    });
    dialog.show();

  }

  public static void showMsgDialog(Activity act, String title, String msg,
      final onSubmitListener listener) {
    final Dialog dialog = new Dialog(act, R.style.mDialog);
    View view = act.getLayoutInflater().inflate(R.layout.dialog_msg, null);
    View layout = view.findViewById(R.id.dialog_layout);
    TextView titleTv = (TextView) view.findViewById(R.id.dialog_title_tv);
    TextView msgTv = (TextView) view.findViewById(R.id.dialog_msg_tv);
    TextView cancelTv = (TextView) view.findViewById(R.id.dialog_cancel_tv);
    TextView subMitTv = (TextView) view.findViewById(R.id.dialog_submit_tv);
    setWidthHeight(layout, DensityUtil.getScreenWidth() * 3 / 4, 0);
    AutoUtil.auto(view);
    dialog.setContentView(view);
    if (!Util.isNull(title)) {
      titleTv.setText(title);
    }
    if (!Util.isNull(msg)) {
      msgTv.setText(msg);
    }
    cancelTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.cancel();
        dialog.dismiss();
      }
    });
    subMitTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.cancel();
        dialog.dismiss();
        if (null != listener) {
          listener.doSubmit();
        }
      }
    });
    dialog.show();
  }

  public static void showDelDialog(Activity act, String title, String msg,
      final onSubmitListener listener) {
    final Dialog dialog = new Dialog(act, R.style.mDialog);
    View view = act.getLayoutInflater().inflate(R.layout.dialog_del, null);
    View layout = view.findViewById(R.id.dialog_layout);
    TextView msgTv = (TextView) view.findViewById(R.id.dialog_msg_tv);
    TextView cancelTv = (TextView) view.findViewById(R.id.dialog_cancel_tv);
    TextView subMitTv = (TextView) view.findViewById(R.id.dialog_submit_tv);
    setWidthHeight(msgTv, DensityUtil.getScreenWidth() * 2 / 3, DensityUtil.dip2px(act, 100));
    setWidthHeight(layout, DensityUtil.getScreenWidth() * 2 / 3, 0);
    AutoUtil.auto(view);
    dialog.setContentView(view);
    if (!Util.isNull(msg)) {
      msgTv.setText(msg);
    }
    if (!Util.isNull(title)) {

    }
    cancelTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.cancel();
        dialog.dismiss();
      }
    });
    subMitTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.cancel();
        dialog.dismiss();
        if (null != listener) {
          listener.doSubmit();
        }
      }
    });
    dialog.show();
  }

  public static void showKandanDialog(Activity act, String title, String msg,
      final onCancelListener cancelListener, final onSubmitListener listener) {
    final Dialog dialog = new Dialog(act, R.style.mDialog);
    View view = act.getLayoutInflater().inflate(R.layout.dialog_kandai, null);
    View layout = view.findViewById(R.id.dialog_kandai_layout_ll);
    TextView titleTv = (TextView) view.findViewById(R.id.dialog_kandai_title_tv);
    TextView msgTv = (TextView) view.findViewById(R.id.dialog_kandai_msg_tv);
    TextView cancelTv = (TextView) view.findViewById(R.id.dialog_kandai_cancel_tv);
    TextView subMitTv = (TextView) view.findViewById(R.id.dialog_kandai_submit_tv);
    setWidthHeight(layout, DensityUtil.getScreenWidth() * 3 / 4, 0);
    AutoUtil.auto(view);
    dialog.setContentView(view);
    if (!Util.isNull(msg)) {
      msgTv.setText(msg);
    }
    if (!Util.isNull(title)) {
      titleTv.setText(title);
    }
    cancelTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.cancel();
        dialog.dismiss();
        if (null != cancelListener) {
          cancelListener.doCancel();
        }
      }
    });
    subMitTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.cancel();
        dialog.dismiss();
        if (null != listener) {
          listener.doSubmit();
        }
      }
    });
    dialog.setCancelable(false);
    dialog.show();
  }

  public static void setWidthHeight(View view, int w, int h) {
    if (null != view && view.getParent() instanceof LinearLayout) {
      LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) view.getLayoutParams();
      if (w == 0) {
        w = ll.WRAP_CONTENT;
      }
      if (h == 0) {
        h = ll.WRAP_CONTENT;
      }
      ll.width = w;
      ll.height = h;
      view.setLayoutParams(ll);
    }
  }

  //    public static void showKandaiSubmitDialog(Activity act,String title,String msg,onSubmitListener listener){
//        final Dialog dialog = new Dialog(act, R.style.mDialog);
//        View view = act.getLayoutInflater().inflate(R.layout.dialog_del, null);
//        View layout = view.findViewById(R.id.dialog_layout);
//        TextView msgTv = (TextView) view.findViewById(R.id.dialog_msg_tv);
//        TextView cancelTv = (TextView) view.findViewById(R.id.dialog_cancel_tv);
//        TextView subMitTv = (TextView) view.findViewById(R.id.dialog_submit_tv);
//        setWidthHeight(msgTv, DensityUtil.getScreenWidth() * 2 / 3, DensityUtil.dip2px(act, 100));
//        setWidthHeight(layout, DensityUtil.getScreenWidth() * 2 / 3, 0);
//        AutoUtil.auto(view);
//        dialog.setContentView(view);
//        if (!Util.isNull(msg)) {
//            msgTv.setText(msg);
//        }
//        cancelTv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.cancel();
//                dialog.dismiss();
//            }
//        });
//        subMitTv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.cancel();
//                dialog.dismiss();
//                if (null != listener) {
//                    listener.subMit();
//                }
//            }
//        });
//        dialog.show();
//    }
  public static void showOrderConfirmDialog(Activity context, String title, String content,
      onSubmitListener listener) {
    final Dialog dialog = new Dialog(context, R.style.mDialog);
    View view = context.getLayoutInflater().inflate(R.layout.dialog_return, null);
    View layout = ButterKnife.findById(view, R.id.layout);
    TextView titleTv = ButterKnife.findById(view, R.id.tv_dialog_title);
    TextView contentTv = ButterKnife.findById(view, R.id.tv_dialog_content);
    TextView cancelTv = ButterKnife.findById(view, R.id.tv_dialog_cancel);
    TextView submitTv = ButterKnife.findById(view, R.id.tv_dialog_submit);
    ViewUtils.setText(contentTv, content);
    ViewUtils.setText(titleTv, title);
    ViewUtils.setLayoutParams(layout,
        ScreenUtil.getWidth() - DensityUtil.dip2px(context, 60),
        -2);
    AutoUtil.auto(view);
    dialog.setContentView(view);
    cancelTv.setOnClickListener(v -> dialog.dismiss());
    submitTv.setOnClickListener(v -> {
      dialog.dismiss();
      if (null != listener) {
        listener.doSubmit();
      }
    });
    dialog.show();

  }
  public static void showIKnowDialog(Activity context, String title, String content) {
    final Dialog dialog = new Dialog(context, R.style.mDialog);
    View view = context.getLayoutInflater().inflate(R.layout.dialog_iknow, null);
    View layout = ButterKnife.findById(view, R.id.layout);
    TextView subMitTv = ButterKnife.findById(view, R.id.submit_tv);
    TextView titleTv = ButterKnife.findById(view, R.id.title_tv);
    TextView contentTv = ButterKnife.findById(view, R.id.content_tv);
    if (!Util.isNull(title)) {
      titleTv.setText(title);
    }
    if (!Util.isNull(content)) {
      contentTv.setText(content);
    }
    LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) layout.getLayoutParams();
    ll.width = ScreenUtil.getWidth() * 3 / 4;
    ll.height = LayoutParams.WRAP_CONTENT;
    layout.setLayoutParams(ll);
    AutoUtil.auto(view);
    dialog.setContentView(view);
    subMitTv.setOnClickListener(v -> dialog.dismiss());
    dialog.show();
  }


  public interface onSubmitListener {

    void doSubmit();
  }

  public interface onCancelListener {

    void doCancel();
  }

}
